﻿using System;
using System.Data;
using System.Data.Linq;

using ZaidimasSandelys;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ZaidimasSandelys.Models.Classes;
using System.Collections.Generic;
using ZaidimasSandelys.Models.DataStructures;
using ZaidimasSandelys.CustomDrawTools;
using System.Threading;
using ZaidimasSandelys.Models.Interfaces;
using System.Configuration;

namespace DiplominioDarboTestai
{
    [TestClass]
    public class UnitTest1
    {
        private static readonly string CONN_STRING_KEY = "ZaidimasSandelys.Properties.Settings.ZaidimasSandelysConnectionString";
        private IAppData appData;
        
        [TestInitialize()]
        public void OpenConnection()
        {
            var connString = ConfigurationManager.ConnectionStrings[CONN_STRING_KEY].ConnectionString;
            appData = new AppSqlCeData(connString);
        }

        [TestMethod]
        public void TestConnection1()
        {
            IEnumerable<MainMenuItem> items = appData.GetMainMenu();

            Assert.IsTrue(items != null);
            foreach (MainMenuItem itm in items)
            {
                System.Console.WriteLine("{0}\t{1}", itm.Text, itm.Value);
            }
        }

        [TestMethod]
        public void TestConnection2()
        {
            IEnumerable<VariantMenuItem> items = appData.GetGameVariants(GameLevel.Medium);

            Assert.IsTrue(items != null);
            foreach (VariantMenuItem itm in items)
            {
                System.Console.WriteLine("{0}\t{1}", itm.Text, itm.Value);
            }
        }

        [TestMethod]
        public void TestConnection3()
        {
            string[] items = appData.GetMultilanguageConstants("Main");
            Assert.IsTrue(items != null && items.Length > 0);
            Array.ForEach(items, x => { System.Console.WriteLine("{0}", x); });

            items = appData.GetMultilanguageConstants("YouWon");
            Assert.IsTrue(items != null && items.Length > 0);
            Array.ForEach(items, x => { System.Console.WriteLine("{0}", x); });
        }

        [TestMethod]
        public void TestLoadGame1()
        {
            var game = new CurrentGame(appData);

            game.LoadGame(0, 1, null);
            Assert.IsNotNull(game.Header);

            System.Console.WriteLine("id={0} Level={1} Variant={2}, X={3} Y={4}",
                game.Header.Id, game.Header.Level, game.Header.Variant,
                (int)game.Header.Person.X, (int)game.Header.Person.Y);
        }
        
        /// <summary>
        /// Žaidimo elementai kraunami tiesiai iš duomenų bazės metodo.
        /// </summary>
        [TestMethod]
        public void TestLoadGame2()
        {
            var game = new CurrentGame(appData);
            var fakeList = new List<IGameElement>();

            game.LoadGame(0, 1, null);
            Assert.IsNotNull(game.Header);

            fakeList.AddRange(appData.LoadVisualElements(game.Header.Id, VisualType.OuterSpace));
            Assert.IsTrue(fakeList.Count > 0);

            System.Console.WriteLine("id={0} Level={1} Variant={2}, X={3} Y={4}",
                game.Header.Id, game.Header.Level, game.Header.Variant,
                (int)game.Header.Person.X, (int)game.Header.Person.Y);
            fakeList.ForEach(x =>
            {
                VisualType visual = AllocatedVisual.SafeConvertToVisualType(x.ImgTypeCode);
                System.Console.WriteLine("\tPoint=({0},{1}) {2}", x.X, x.Y, visual.ToString());
            });
        }

        //private List<AllocatedVisual> fakeList = null;
        //private AutoResetEvent event_1 = null;  //
        /// <summary>
        /// Išorinius elementus asinchroniškai užkrauna žaidimo procesorius, <see cref="CurrentGame"/>
        /// </summary>
        [TestMethod]
        public void TestLoadGame3()
        {
            var fakeList = new List<AllocatedVisual>();
            var event_1 = new AutoResetEvent(false);  //

            var game = new CurrentGame(appData);
//            game.SetFlagTest(0x0001);           // Skaityti tik išorinius laukus
//            game.SetFlagTest(0x0002);           // Skaityti tik sienas
//            game.SetFlagTest(0x0004);           // Skaityti tik grindis
//            game.SetFlagTest(0x0008);           // Skaityti tik taikinius (langeliai kuriuose talpinamos dėžės)
//            game.SetFlagTest(0x0010);           // Skaityti tik dėžes
            game.LoadCompleted += delegate(object sender, LoadCompletedEventArgs e)
                {
                    Assert.IsNotNull(game.Header);
                    Assert.IsTrue(fakeList.Count > 0);
                    int i = 1;

                    fakeList.ForEach(x =>
                    {
                        System.Console.WriteLine("{0}\tPoint=({1},{2}) {3}", i++, x.Position.Width, x.Position.Height, x.GetType().Name);
                    });
                    event_1.Set();
                };
            
            game.LoadGame(0, 1, fakeList);
            event_1.WaitOne();
        }
    }
}
